import main from '../views/main.vue'
import Util from './util';
//import ParentView from '@/components/parent-view'
// 加载路由菜单,从localStorage拿到路由,在创建路由时使用
//  @函数: 引入组件
export const lazyLoadingCop = file => require('../views' + file + '.vue').default
class RouterHelper {
  dynamicRouterAdd(): Array<Router> {
    let dynamicRouter = []
    console.log(window.abp.nav.menus.MainMenu)
    window.abp.nav.menus.MainMenu.items.forEach(el => {
      //console.log(el.displayName)
      //console.log(window.abp.auth.hasPermission(el.customData.requiredPermissionName));
      //console.log(Util.abp.auth.hasPermission(el.customData.requiredPermissionName))
      let obj = {
        //path: '/' + el.name,
        path: el.url,
        name: el.name,
        icon: el.icon,
        permission: el.customData.requiredPermissionName,
        meta: { title: el.customData.displayName },
        component: el.url,
        children: [],

      }
      if (el.items.length > 0) {

        // el.items.forEach(child => {
        //   obj.children.push({
        //     path: child.name,
        //     name: child.name,
        //     icon: child.icon,
        //     permission: "Pages.Users1",
        //     meta: { title: child.displayName },
        //     component: child.url,
        //     children: null,
        //   })
        // })
        // dynamicRouter.push(obj)

        obj.children = this.routersChildren(el.items)

      }
      dynamicRouter.push(obj)
    });
    dynamicRouter = this.filterAsyncRouter(dynamicRouter, 1, true)
    dynamicRouter = this.hasPermission(dynamicRouter)
    // console.log("console.log(dynamicRouter)")
    console.log(dynamicRouter)
    return dynamicRouter
  }
  //递归实现返回路由的下级菜单--实现多级菜单
  routersChildren(objRc) {
    var objArry = [];
    objRc.forEach(child => {
      var obj =
      {
        path: child.name,
        name: child.name,
        icon: child.icon,
        permission: child.customData.requiredPermissionName,
        meta: { title: child.customData.displayName },
        component: child.url,
        children: null,
      }
      // console.log(Util.abp.auth.hasPermission(obj.permission))
      if (child.items.length > 0) {
        obj.children = this.routersChildren(child.items)
      }
      objArry.push(obj)
    })
    return objArry;
  }
  // @函数: 遍历后台传来的路由字符串，转换为组件对象 -一级菜单 mian,几级菜单。上级 ，是不是菜单 
  filterAsyncRouter(asyncRouterMap, lv, IsUpMain) {
    const accessedRouters = asyncRouterMap.filter(route => {
      if (lv == 2) {

      }
      let CurrIsUpMain = false;
      try {
        if (IsUpMain == true && route.children != null && route.children.length > 0) {
          switch (lv) {
            case 1:
              route.component = main
              break;
            case 2:
              route.component = lazyLoadingCop("/layout/lv" + lv)
              break;
          }
          CurrIsUpMain = true;
        } else {
          route.component = lazyLoadingCop(route.component)
        }
      } catch (Ex) {
        route.component = main
      }
      if (route.children && route.children.length) {
        var Lvv = lv + 1;
        route.children = this.filterAsyncRouter(route.children, Lvv, CurrIsUpMain)
      }

      // if (route.component) {
      //   if (route.children==null|| route.children.length>0 ) { // Main组件特殊处理
      //     switch(lv){   
      //       case 1:
      //         route.component = main
      //         break;
      //       case 2:
      //         route.component = main
      //         break;
      //     }
      //     //如果没有子菜单
      //   } 
      //   else {
      //     route.component = lazyLoadingCop(route.component)
      //   }
      // }
      // if (route.children && route.children.length) {
      //   var Lvv=lv+1;
      //   route.children = this.filterAsyncRouter(route.children,Lvv)
      // }
      return true
    })
    return accessedRouters
  }
  //权限判断
  hasPermission(asyncRouterMap) {
    const accessedRouters = asyncRouterMap.filter(route => {

      if (route.children && route.children.length > 0) {//二级
        route.children = route.children.filter(r2 => {
          if (r2.children && r2.children.length > 0) {//三级级
            r2.children = r2.children.filter(r3 => {
              console.log(r3.permission)
              return Util.abp.auth.hasPermission(r3.permission)
            })

          } else { //二级
            return Util.abp.auth.hasPermission(r2.permission)
          }

          if (!(r2.children && r2.children.length > 0)) { //三级 本来是有的但是全部过滤就没有了
            return false;
          } else {
            return true;
          }

        })

      } else { //一级
        return Util.abp.auth.hasPermission(route.permission)
      }
      if (!(route.children && route.children.length > 0)) { //二级 本来是有的但是全部过滤就没有了
        return false;
      }

      return true
    })
    return accessedRouters
  }

}
const routerHelper = new RouterHelper();
export default routerHelper;